<?php

ini_set('date.timezone','Asia/Shanghai');

/***************************************************************
 * Demo用的易极付账户信息
 **************************************************************/
//卖家信息 seller TODO: 修改为你自己的卖家信息
$user['seller_uid_yiji'] = "20160606020000748137";          //卖家易极付userId
$user['seller_id'] = "ptsu1234567";                //卖家商户平台上的会员Id
$user['seller_name'] = "ydianna1";                          //卖家商户平台上的会员名称

//买家信息 buyer TODO: 修改为你自己的买家信息
$user['buyer_uid_yiji'] = "20160612020000748352";           //买家易极付userId
$user['buyer_id'] = "ptbu1234567";                 //买家商户平台上的会员Id
$user['buyer_name'] = "ydianna";                            //买家商户平台上的会员名称


/***************************************************************
 * Demo用的工具函数
 **************************************************************/

/**
 * 注册自动load classes
 * @param $class
 */
function loader($class) {
    $file = '../' . $class . '.class.php';
    if (is_file($file)) {
        require_once($file);
    }
}
spl_autoload_register('loader');

/**
 * 测试用函数，不建议用于生产
 * @return string 18位
 */
function genOrderNo(){
    return date('YmdHis' . rand(1000, 9999));
}

/**
 * demo
 * 获取returnUrl
 * @return string
 */
function getReturnUrl(){
    $PHP_SELF=$_SERVER['PHP_SELF'];
    $url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
    return $url."myreturn.php";
}
/**
 * demo
 * 获取notifyUrl
 * @return string
 */
function getNotifyUrl(){
    $PHP_SELF=$_SERVER['PHP_SELF'];
    $url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
    return $url."mynotify.php";
}

/**
 * @param $req \yijipay\message\IRequest
 * @return string
 */
function buildHtml($req){
    $sHtml = "<form id='yijiForm' name='yijiForm' method='POST'>\n";
    $params = $req->getVars();
    reset($params);
    while (list ($key, $val) = each ($params)) {
        if(is_array($val) || is_object($val)){
            $val = json_encode($val, JSON_UNESCAPED_UNICODE);
        }else if(is_bool($val)){
            if($val) $val="true"; else $val="false";
        }
        $val = str_replace("'","&apos;",$val);
        $sHtml .= "<input type='text' name='".$key."' value='".$val."'/><br/>\n";
    }
    //submit按钮控件请不要含有name属性
    $sHtml .= "<input type='submit' value='提交'></form>\n";
    return $sHtml;
}

/**
 * 获取客户端IP
 * @return null|string
 */
function get_client_ip()
{
    foreach (array(
                 'HTTP_CLIENT_IP',
                 'HTTP_X_FORWARDED_FOR',
                 'HTTP_X_FORWARDED',
                 'HTTP_X_CLUSTER_CLIENT_IP',
                 'HTTP_FORWARDED_FOR',
                 'HTTP_FORWARDED',
                 'REMOTE_ADDR') as $key) {
        if (array_key_exists($key, $_SERVER)) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                $ip = trim($ip);
                //会过滤掉保留地址和私有地址段的IP，例如 127.0.0.1会被过滤
                //也可以修改成正则验证IP
                if ((bool)filter_var($ip, FILTER_VALIDATE_IP,
                    FILTER_FLAG_IPV4 |
                    FILTER_FLAG_NO_PRIV_RANGE |
                    FILTER_FLAG_NO_RES_RANGE)
                ) {
                    return $ip;
                }
            }
        }
    }
    return null;
}

